//
//  ChangeRoleViewController.m
//  FaceRecognition
//
//  Created by Sylar on 2016/12/5.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "ChangeRoleViewController.h"

@interface ChangeRoleViewController ()<UITableViewDelegate>
{
    NSString * markable;
    NSNotificationCenter * notificationCenter;
    
    SYAccount *accountDic;
    
    UIImageView *tHeadImage;
    
    UIImageView *pheadImage;
    
    UILabel *tLabel01;
    
    UILabel *tLabel02;
    
    UILabel *pLabel01;
    
    UILabel *pLabel02;
    
    NSString *havestudent;
    NSString *haveclass;
    
    NSString  *headImgstr;
}

@property (strong, nonatomic) UIButton *selectTeacherRoleBtn;
@property (strong, nonatomic) UIButton *selectGuardianBtn;

@end

@implementation ChangeRoleViewController

- (void)loadView {
    
    [super loadView];
    
    self.navBar.nav_titleLabel.text = @"角色切换";
    
    havestudent = @"0";
    haveclass= @"0";
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    accountDic=[SYAccountTool ShareAccount].account;
    markable = [NSString stringWithFormat:@"%d",accountDic.lasttype];
    headImgstr=[NSString stringWithFormat:@"%@%@",REQUESTIMAGEURL,accountDic.logo];
    
//    [self getStudentListData];
//    [self loadAllClasses];
    
    [self setUpView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)setUpView {
    
    tHeadImage = [[UIImageView alloc]initWithFrame:CGRectMake(15, 8+SafeAreaTopHeight, 40, 40)];
    tHeadImage.layer.cornerRadius=19;
    tHeadImage.clipsToBounds = YES;
    [self.view addSubview:tHeadImage];
    
    tLabel01 = [[UILabel alloc]initWithFrame:CGRectMake(60, 12+SafeAreaTopHeight, 100, 15)];
    [tLabel01 setFont:[UIFont systemFontOfSize:15]];
    tLabel01.textColor =colorFromHex(@"#3b3b3b");
    tLabel01.text = @"我是老师";
    
    [self.view addSubview:tLabel01];
    
    tLabel02 = [[UILabel alloc]initWithFrame:CGRectMake(60, tLabel01.bottom + 6, 253, 13)];
    [tLabel02 setFont:[UIFont systemFontOfSize:13]];
    tLabel02.textColor = colorFromHex(@"#979797");
    tLabel02.text = @"查看本班识别记录，管理底片";
    
    [self.view addSubview:tLabel02];
    
    self.selectTeacherRoleBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.selectTeacherRoleBtn setFrame:CGRectMake(SCREENWIDTH - 42, SafeAreaTopHeight, 42, 58)];
    [self.selectTeacherRoleBtn setImage:[UIImage imageNamed:@"homeworks_allchoose_nor_"] forState:(UIControlStateNormal)];
    [self.selectTeacherRoleBtn setImage:[UIImage imageNamed:@"homeworks_allchoose_press_"] forState:(UIControlStateSelected)];
    self.selectTeacherRoleBtn.tag = 11;
    [self.selectTeacherRoleBtn addTarget:self action:@selector(changeRole:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.selectTeacherRoleBtn];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(15, tLabel02.bottom+12, SCREENHEIGH, 0.5)];
    lineView.backgroundColor = colorFromHex(@"#DEDEDE");
    [self.view addSubview:lineView];
    
    
    pheadImage = [[UIImageView alloc]initWithFrame:CGRectMake(15, lineView.bottom + 8, 40, 40)];
    pheadImage.layer.cornerRadius=19;
    pheadImage.clipsToBounds = YES;
    [self.view addSubview:pheadImage];
    
    pLabel01 = [[UILabel alloc]initWithFrame:CGRectMake(60, lineView.bottom + 12, 100, 15)];
    [pLabel01 setFont:[UIFont systemFontOfSize:15]];
    pLabel01.textColor = colorFromHex(@"#3b3b3b");
    pLabel01.text = @"我是家长";
    
    [self.view addSubview:pLabel01];
    
    pLabel02 = [[UILabel alloc]initWithFrame:CGRectMake(60, pLabel01.bottom + 6, 253, 13)];
    [pLabel02 setFont:[UIFont systemFontOfSize:13]];
    pLabel02.textColor = colorFromHex(@"#979797");
    pLabel02.text = @"查看孩子识别记录，管理底片";
    
    [self.view addSubview:pLabel02];
    
    self.selectGuardianBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.selectGuardianBtn setFrame:CGRectMake(SCREENWIDTH - 42,lineView.bottom, 42, 58)];
    [self.selectGuardianBtn setImage:[UIImage imageNamed:@"homeworks_allchoose_nor_"] forState:(UIControlStateNormal)];
    [self.selectGuardianBtn setImage:[UIImage imageNamed:@"homeworks_allchoose_press_"] forState:(UIControlStateSelected)];
    self.selectGuardianBtn.tag = 10;
    [self.selectGuardianBtn addTarget:self action:@selector(changeRole:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:self.selectGuardianBtn];
    
    UIView *btmlineView = [[UIView alloc]initWithFrame:CGRectMake(0, lineView.bottom + 58, SCREENHEIGH, 0.5)];
    btmlineView.backgroundColor = colorFromHex(@"#DEDEDE");
    [self.view addSubview:btmlineView];
    
    [self displaySubviewWithMark:markable];
    
}

- (void)displaySubviewWithMark:(NSString *)type
{
    if ([type isEqualToString:@"1"]) {
        
        _selectTeacherRoleBtn.selected=YES;
        _selectGuardianBtn.selected=NO;
        [tHeadImage sd_setImageWithURL:[NSURL URLWithString:headImgstr]];
        pheadImage.image=[UIImage imageNamed:@"personalsettings_switchloading_pic_"];
        pLabel01.textColor=RGBCOLOR(151, 151, 151);
        pLabel02.textColor=RGBCOLOR(151, 151, 151);
        tLabel01.textColor=RGBCOLOR(59, 59, 59);
        tLabel02.textColor=RGBCOLOR(151, 151, 151);
        
        
    }else if ([type isEqualToString:@"2"]){
        
        _selectTeacherRoleBtn.selected=NO;
        _selectGuardianBtn.selected=YES;
        [pheadImage sd_setImageWithURL:[NSURL URLWithString:headImgstr]];
        tHeadImage.image=[UIImage imageNamed:@"personalsettings_switchloading_pic_"];
        tLabel01.textColor=RGBCOLOR(151, 151, 151);
        tLabel02.textColor=RGBCOLOR(151, 151, 151);
        pLabel01.textColor=RGBCOLOR(59, 59, 59);
        pLabel02.textColor=RGBCOLOR(151, 151, 151);
        
    }
}

- (void)changeRole:(UIButton *)sender {
    
    if (sender.tag==10) {

        if ([markable isEqualToString:@"2"]){
            return;
        }
        
        [self getStudentListData];
    }
    
    if (sender.tag==11) {
        
        if ([markable isEqualToString:@"1"]){
            return;
        }
        
        [self loadAllClasses];
    }
    
}

#pragma mark -切换角色，从新登录
- (void)relogin{
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_user_login" forKey:@"method"];
    [dict setObject:accountDic.mobile forKey:@"loginname"];
    [dict setObject:accountDic.loginpass forKey:@"userpass"];
    
    [Utils get:nil parameters:dict success:^(NSInteger recode, id resposeJson){
        
        NSDictionary * dict = (NSDictionary *)resposeJson;
        
        //判断是否开通平安家校
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        [param setObject:USERID forKey:@"userid"];
        [Utils Java_get:@"api/isOpenFace" parameters:param success:^(NSInteger recode, id jsonObject) {
            
            NSDictionary *jsonDict = (NSDictionary *)jsonObject;
            NSDictionary *re_data=[jsonDict objectForKey:@"re_data"];
            
            if ([[re_data objectForKey:@"openface"] integerValue] == 1) {
                
                NSMutableDictionary *personalImformation = [[NSMutableDictionary alloc]initWithDictionary: [dict objectForKey:@"re_data"]];
                if (personalImformation.count>0) {
                    
                    [personalImformation setObject:[NSString stringWithFormat:@"%d",[[re_data objectForKey:@"faceuserid"] intValue]] forKey:@"faceuserid"];
                   [personalImformation setObject:[NSString stringWithFormat:@"%@",[re_data objectForKey:@"facetype"]] forKey:@"facetype"];
                   
                    
                    NSMutableDictionary * userinfo = [[NSMutableDictionary alloc]initWithDictionary: [personalImformation objectForKey:@"userinfo"]];
                    [userinfo setObject:[NSString stringWithFormat:@"%d",[[re_data objectForKey:@"faceuserid"] intValue]] forKey:@"faceuserid"];
                    [userinfo setObject:[NSString stringWithFormat:@"%@",[re_data objectForKey:@"facetype"]] forKey:@"facetype"];
                   
                    [personalImformation setObject:userinfo forKey:@"userinfo"];
                    
                    //存入scid
                    NSString *scid = [[re_data objectForKey:@"scid"] stringValue];
                    [personalImformation setObject:scid forKey:@"scid"];
                    
                    [personalImformation setObject:[NSString stringWithFormat:@"%ld",[SYAccountTool ShareAccount].account.haveclass] forKey:@"haveclass"];
                }
                
                [SyUserInfomation personalImformationSaveWtih:personalImformation];
                
                //因为切换了角色，老师身份和家长身份的通知会不同，所以要让主页控制器先刷新一下数据
                [[NSNotificationCenter defaultCenter] postNotificationName:@"switchrolenoti" object:nil userInfo:nil];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeHomeUI" object:nil userInfo:nil];
                
                [self.navigationController popViewControllerAnimated:YES];
                
            }else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"此账号未开通平安家校" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil,nil];
                
                [alert show];
            }
            
            
            
            
        } failure:^(NSInteger recode, NSError * error){
            
        }];
        
    }failure:^(NSInteger recode, NSError *error) {
        
    }];
}
-(void)saveStudentID
{
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_parentstudent_list" forKey:@"method"];
    [dict setObject:@"20" forKey:@"pagesize"];
    [dict setObject:USERID forKey:@"parentid"];
    [dict setObject:@"1" forKey:@"pageno"];
    [Utils get:nil parameters:dict success:^(NSInteger recode, id resposeJson){
        NSDictionary * dict = (NSDictionary *)resposeJson;
        if ([[dict objectForKey:@"re_code"] integerValue]==0) {
            if ([[dict objectForKey:@"re_data"] count]>0) {
                NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
                [ud setObject:[[[dict objectForKey:@"re_data"] objectAtIndex:0] objectForKey:@"userid"] forKey:@"studentID"];
                [ud synchronize];
            }
        }
        
        //因为切换了角色，老师身份和家长身份的通知会不同，所以要让主页控制器先刷新一下数据
        [[NSNotificationCenter defaultCenter] postNotificationName:@"switchrolenoti" object:nil userInfo:nil];
        
        
    } failure:^(NSInteger recode, NSError * error){
        
    }];
}

- (void)loadAllClasses {
    
    NSMutableDictionary * paramDic = [[NSMutableDictionary alloc] initWithCapacity:0];
    [paramDic setObject:USERID forKey:@"teacherid"];
    [paramDic setObject:SCHOOLID forKey:@"scid"];
    [paramDic setObject:[SYAccountTool ShareAccount].account.schoolManage > 0?@"1":@"0" forKey:@"type"];
    
    
    [Utils Java_get:@"api/getTeacherClass" parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        
        if (arr.count == 0 && [SYAccountTool ShareAccount].account.schoolManage == 0 && [SYAccountTool ShareAccount].account.schoolnum == 0) {
            
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"您暂无管理权限" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            return;
            
        }else { 
            
            NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
            [dict setObject:@"api_user_logintype" forKey:@"method"];
            [dict setObject:USERID forKey:@"userid"];
            
            if (![markable isEqualToString:@"1"]) {
                
                [dict setObject:@"1" forKey:@"type"];
            }else {
                return;
            }
            
            [Utils get:nil parameters:dict success:^(NSInteger recode, id resposeJson){
                AppDelegate * appdelegate = CONSTAPPDELEGATE;
                appdelegate.isChangeRole=@"yes";
                appdelegate.studentId = nil;
                appdelegate.myClasses = nil;
                
                [self relogin];
            } failure:^(NSInteger recode, NSError * error){
                
            }];
            
        }
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
}

- (void)getStudentListData {
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:USERID forKey:@"parentid"];
    
    [Utils Java_get:@"api/getChildClass" parameters:dict success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary * dict = (NSDictionary *)jsonObject;
        if ([[dict objectForKey:@"re_code"] integerValue]==0) {
            
            if ([[dict objectForKey:@"re_data"] count]>0) {
                
                NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
                [dict setObject:@"api_user_logintype" forKey:@"method"];
                [dict setObject:USERID forKey:@"userid"];
                
                if (![markable isEqualToString:@"2"]) {
                    [dict setObject:@"2" forKey:@"type"];
                    
                }else {
                    return;
                }
                
                [Utils get:nil parameters:dict success:^(NSInteger recode, id resposeJson){
                    AppDelegate * appdelegate = CONSTAPPDELEGATE;
                    appdelegate.isChangeRole=@"yes";
                    appdelegate.studentId = nil;
                    appdelegate.myClasses = nil;
                    
                    [self relogin];
                } failure:^(NSInteger recode, NSError * error){
                    
                }];
                
            }else {
                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"您暂未关联孩子，或您的孩子未开通平安家校" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alert show];
                    return;
            }
            
        }
        
    }failure:^(NSInteger recode, NSError *error) {
        [self showHint:error.localizedDescription];
    }];
    
    
}

@end
